﻿using System;
using System.Diagnostics;
using System.Threading.Tasks;
using CN2P.Web.Db;

namespace CN2P.Web.Controllers.Api.Proxy
{
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using CN2P.ServiceModel;

    public class CartApiProxyController : BaseApiProxyController
    {
        [AcceptVerbs("GET")]
        public CartModel Get(string userId)
        {
            var decodedUserId = this.DecodeUserId(userId);
            return client.CartService.GetCart(decodedUserId);
        }

        [AcceptVerbs("POST")]
        public HttpResponseMessage Post(string userId, CartModel model)
        {
            var decodedUserId = this.DecodeUserId(userId);
            client.CartService.AddCart(decodedUserId, model);

            return Request.CreateResponse(HttpStatusCode.Created);
        }

        [AcceptVerbs("DELETE")]
        public HttpResponseMessage Delete(string userId)
        {
            var decodedUserId = this.DecodeUserId(userId);
            client.CartService.DeleteCart(decodedUserId);

            return Request.CreateResponse(HttpStatusCode.Created);
        }

        [AcceptVerbs("POST")]
        public HttpResponseMessage PostItem(string userId, CartItemModel model)
        {
            var decodedUserId = this.DecodeUserId(userId);
            client.CartService.PostItem(decodedUserId, model);

            return Request.CreateResponse(HttpStatusCode.Created);
        }

        [AcceptVerbs("PUT")]
        public HttpResponseMessage PutItem(string userId, CartItemModel model)
        {
            var decodedUserId = this.DecodeUserId(userId);
            client.CartService.PutItem(decodedUserId, model.ProdId, model);

            return Request.CreateResponse(HttpStatusCode.Created);
        }

        [AcceptVerbs("DELETE")]
        public HttpResponseMessage DeleteItem(string userId, string prodId)
        {
            var decodedUserId = this.DecodeUserId(userId);
            client.CartService.DeleteItem(decodedUserId, prodId);

            return Request.CreateResponse(HttpStatusCode.Created);
        }


        [AcceptVerbs("POST")]
        public HttpResponseMessage Checkout(string userId)
        {
            var decodedUserId = this.DecodeUserId(userId);

            var sw = Stopwatch.StartNew();
            var cart = client.CartService.GetCart(decodedUserId);
            sw.Stop();

            var orderId = "";

            var sw1 = Stopwatch.StartNew();
            var orderModel = cart.CreateOrder();
            var order = Order.From(orderModel);

            using (var context = new ECommerceDataContext())
            {
                context.Orders.InsertOnSubmit(order);
                context.SubmitChanges();
                orderId = order.Id;
            }
            sw1.Stop();

            var response = Request.CreateResponse(HttpStatusCode.Created);

            //Task.Run(() => client.CartService.DeleteCart(decodedUserId));

            var sw2 = Stopwatch.StartNew();
            client.CartService.DeleteCart(decodedUserId);
            sw2.Stop();

            //if (!string.IsNullOrWhiteSpace(orderId))
            //{
            //    response.Headers.Location = new Uri(string.Format("{0}api/orders/{1}/reviews/{2}", OrdersUri, userId, orderId));
            //}

            return response;
        }
    }
}